﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ihongma.Common;
using ihongma.API.Payment.WapAlipay;
using System.Text;

namespace ihongma.Web.api.payment.wapalipay
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //读取站点配置信息
            Model.siteconfig siteConfig = new BLL.siteconfig().loadConfig();

            //=============================获得订单信息================================
            string order_no = DTRequest.GetFormString("pay_order_no").ToUpper();
            decimal order_amount = DTRequest.GetFormDecimal("pay_order_amount", 0);
            string user_name = DTRequest.GetFormString("pay_user_name");
            string subject = DTRequest.GetFormString("pay_subject");
            string pay_user_type = DTRequest.GetFormString("pay_user_type");
            //以下收货人信息
            string receive_name = string.Empty; //收货人姓名
            string receive_address = string.Empty; //收货人地址
            string receive_zip = string.Empty; //收货人邮编
            string receive_phone = string.Empty; //收货人电话
            string receive_mobile = string.Empty; //收货人手机
            //检查参数是否正确
            if (order_no == "" || order_amount == 0)
            {
                Response.Redirect(new Web.UI.BasePage().linkurl("error", "?msg=" + Utils.UrlEncode("对不起，您提交的参数有误！")));
                return;
            }
            if (order_no.StartsWith("R")) //R开头为在线充值订单
            {
                Model.user_amount_log model = new BLL.user_amount_log().GetModel(order_no);
                if (model == null)
                {
                    Response.Redirect(new Web.UI.BasePage().linkurl("error", "?msg=" + Utils.UrlEncode("对不起，您充值的订单号不存在或已删除！")));
                    return;
                }
                if (model.value != order_amount)
                {
                    Response.Redirect(new Web.UI.BasePage().linkurl("error", "?msg=" + Utils.UrlEncode("对不起，您充值的订单金额与实际金额不一致！")));
                    return;
                }
                //取得用户信息
                Model.users userModel = new BLL.users().GetModel(model.user_id);
                if (userModel == null)
                {
                    Response.Redirect(new Web.UI.BasePage().linkurl("error", "?msg=" + Utils.UrlEncode("对不起，用户账户不存在或已删除！")));
                    return;
                }
                receive_name = userModel.nick_name;
                receive_address = userModel.address;
                receive_phone = userModel.telphone;
                receive_mobile = userModel.mobile;
            }
            else //B开头为商品订单
            {
                Model.orders model = new BLL.orders().GetModel(order_no);
                if (model == null)
                {
                    Response.Redirect(new Web.UI.BasePage().linkurl("error", "?msg=" + Utils.UrlEncode("对不起，您支付的订单号不存在或已删除！")));
                    return;
                }
                if (model.order_amount != order_amount)
                {
                    Response.Redirect(new Web.UI.BasePage().linkurl("error", "?msg=" + Utils.UrlEncode("对不起，您支付的订单金额与实际金额不一致！")));
                    return;
                }
                receive_name = model.accept_name;
                receive_address = model.address;
                receive_zip = model.post_code;
                receive_phone = model.telphone;
                receive_mobile = model.mobile;
            }
            if (user_name != "")
            {
                user_name = "支付会员：" + user_name;
            }
            else
            {
                user_name = "匿名用户";
            }

            //===============================请求参数==================================
            
            //判断担保或是即时到帐接口
            if (Config.Type == "1") //即时到帐
            {
              ;
               
             

                //支付宝网关地址
                string GATEWAY_NEW = "http://wappaygw.alipay.com/service/rest.htm?";

                ////////////////////////////////////////////调用授权接口alipay.wap.trade.create.direct获取授权码token////////////////////////////////////////////

                //返回格式
                string format = "xml";
                //必填，不需要修改

                //返回格式
                string v = "2.0";
                //必填，不需要修改

                //请求号
                string req_id = order_no;
                //必填，须保证每次请求都是唯一

                //req_data详细信息

                //服务器异步通知页面路径
                string notify_url = Config.Notify_url;
                //需http://格式的完整路径，不允许加?id=123这类自定义参数

                //页面跳转同步通知页面路径
                string call_back_url = Config.Return_url;
                //需http://格式的完整路径，不允许加?id=123这类自定义参数

                //操作中断返回地址
                string merchant_url = "http://" + HttpContext.Current.Request.Url.Authority.ToLower() + "/mobile.aspx";
                //用户付款中途退出返回商户的地址。需http://格式的完整路径，不允许加?id=123这类自定义参数

                //卖家支付宝帐户
                string seller_email = Config.Email;
                //必填

                //商户订单号
                string out_trade_no =order_no;
                //商户网站订单系统中唯一订单号，必填

                //订单名称
               subject = siteConfig.webname + "-" + subject;
                //必填

                //付款金额
                string total_fee = order_amount.ToString();
                //必填

                //请求业务参数详细
                string req_dataToken = "<direct_trade_create_req><notify_url>" + notify_url + "</notify_url><call_back_url>" + call_back_url + "</call_back_url><seller_account_name>" + seller_email + "</seller_account_name><out_trade_no>" + out_trade_no + "</out_trade_no><subject>" + subject + "</subject><total_fee>" + total_fee + "</total_fee><merchant_url>" + merchant_url + "</merchant_url></direct_trade_create_req>";
                //必填

                //把请求参数打包成数组
                Dictionary<string, string> sParaTempToken = new Dictionary<string, string>();
                sParaTempToken.Add("partner", Config.Partner);
                sParaTempToken.Add("_input_charset", Config.Input_charset.ToLower());
                sParaTempToken.Add("sec_id", Config.Sign_type.ToUpper());
                sParaTempToken.Add("service", "alipay.wap.trade.create.direct");
                sParaTempToken.Add("format", format);
                sParaTempToken.Add("v", v);
                sParaTempToken.Add("req_id", req_id);
                sParaTempToken.Add("req_data", req_dataToken);

                //建立请求
                string sHtmlTextToken = Submit.BuildRequest(GATEWAY_NEW, sParaTempToken);
                //URLDECODE返回的信息
                Encoding code = Encoding.GetEncoding(Config.Input_charset);
                sHtmlTextToken = HttpUtility.UrlDecode(sHtmlTextToken, code);

                //解析远程模拟提交后返回的信息
                Dictionary<string, string> dicHtmlTextToken = Submit.ParseResponse(sHtmlTextToken);

                //获取token
                string request_token = dicHtmlTextToken["request_token"];

                ////////////////////////////////////////////根据授权码token调用交易接口alipay.wap.auth.authAndExecute////////////////////////////////////////////


                //业务详细
                string req_data = "<auth_and_execute_req><request_token>" + request_token + "</request_token></auth_and_execute_req>";
                //必填

                //把请求参数打包成数组
                Dictionary<string, string> sParaTemp = new Dictionary<string, string>();
                sParaTemp.Add("partner", Config.Partner);
                sParaTemp.Add("_input_charset", Config.Input_charset.ToLower());
                sParaTemp.Add("sec_id", Config.Sign_type.ToUpper());
                sParaTemp.Add("service", "alipay.wap.auth.authAndExecute");
                sParaTemp.Add("format", format);
                sParaTemp.Add("v", v);
                sParaTemp.Add("req_data", req_data);

                //建立请求
                string sHtmlText = Submit.BuildRequest(GATEWAY_NEW, sParaTemp, "get", "确认");
                Response.Write(sHtmlText);
            }
            
            else //担保交易
            {
                ////把请求参数打包成数组
                //SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
                //sParaTemp.Add("payment_type", "1"); //收款类型1商品购买
                //sParaTemp.Add("out_trade_no", order_no); //网站订单号
                //sParaTemp.Add("subject", siteConfig.webname + "-" + subject); //订单名称
                //sParaTemp.Add("price", order_amount.ToString()); //付款金额
                //sParaTemp.Add("quantity", "1"); //建议默认为1，不改变值，把一次交易看成是一次下订单而非购买一件商品
                //sParaTemp.Add("logistics_fee", "0.00"); //物流费用
                //sParaTemp.Add("logistics_type", "EXPRESS"); //物流类型，EXPRESS（快递）、POST（平邮）、EMS（EMS）
                //sParaTemp.Add("logistics_payment", "SELLER_PAY"); //物流支付方式，SELLER_PAY(卖家承担运费)、BUYER_PAY(买家承担运费)
                //sParaTemp.Add("body", user_name); //订单描述
                //sParaTemp.Add("show_url", siteConfig.weburl); //商品展示地址
                //sParaTemp.Add("receive_name", receive_name); //收货人姓名
                //sParaTemp.Add("receive_address", receive_address); //收货人地址
                //sParaTemp.Add("receive_zip", receive_zip); //收货人邮编
                //sParaTemp.Add("receive_phone", receive_phone); //收货人电话号码
                //sParaTemp.Add("receive_mobile", receive_mobile); //收货人手机号码
                ////构造即时到帐接口表单提交HTML数据，无需修改
                //Service ali = new Service();
                //string sHtmlText = ali.Create_partner_trade_by_buyer(sParaTemp);
                //Response.Write(sHtmlText);
            }
        }
    }
}